<?php
/**
 * 园所管理
 * @author a
 *
 */
class Nursery extends CT_Controller{
    
    private $offse = 0;
    private $page_size = 0;
    private $page = 0;
    private $size = 10; //每页显示数量
    
    function __construct()
    {
        parent::__construct();
        $this->load->helper("arraytool");   //加载工具
        $this->load->helper("tools_helper");   //加载工具
        $this->t = $this->ct_template;
        
        $this->_nursery_id = 1;     //重置园所ID为1
        
        $this->load->model('nursery_model');
        $this->load->model('area_model');
        $this->load->model('city_model');
        
        //分页
        $this->page = $this->input->get('per_page',TRUE) ? intval($this->input->get('per_page',TRUE)) : 1;
        $this->page_size = $this->config->item('per_page')?$this->config->item('per_page'):$this->size;
        $this->offse = ($this->page-1) * $this->page_size;
        
    }
    
    public function Index(){
        try {
        	
            $nursery_list = $this->nursery_model->findAll(array('status'=>1),'*','weight desc',$this->offse,$this->page_size);
            if(empty($nursery_list))throw new Exception('没有记录');
            
            $total = $this->nursery_model->findCount(array()); //总数
            $pagelink = $this->pagination($total,$this->page_size);
            
            //获取地区
            $where = "id IN (". filterValue($nursery_list,'area_id',true) .")";
            $area_list = $this->area_model->findAll($where,'id,title as area_title');
            $area_list = restByKey($area_list,'id');
            $nursery_list = arrayMerge($nursery_list,'area_id',$area_list);
            
            //获取城市
            $where = "id IN (". filterValue($nursery_list,'city_id',true) .")";
            $city_list = $this->city_model->findAll($where,'id,title as city_title');
            $city_list = restByKey($city_list,'id');
            $nursery_list = arrayMerge($nursery_list,'city_id',$city_list);
            
            //$this->pr($nursery_list);
            
        	$this->t->write_view('content', 'admin/nursery/index', array(
        			'pagelink'     =>  $pagelink,
        			'nursery_list'    =>  $nursery_list,
        	), false);
        	 
        	$this->t->render();
        	        	
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                //'url'   =>  '/admin/nursery/',
            ), false);
            $this->t->render();exit();
        }
    }
    /**
     * 修改
     */ 
    public function Update(){
        try {
            $id = intval($_REQUEST['id']);
            if(empty($id))throw new Exception('ID为空');
            $form_submit = $this->input->post('form_submit');
            if(!empty($form_submit)){
            	$data = array();
            	$data['title'] = $this->input->post('title');
            	$data['area_id'] = intval($this->input->post('area_id'));
            	$data['city_id'] = intval($this->input->post('city_id'));
            	$data['address'] = $this->input->post('address');
            	$data['explain'] = $this->input->post('explain');
            	$data['telephone'] = $this->input->post('telephone');
            	$data['is_allow_login'] = $this->input->post('is_allow_login');
            	$data['is_allow_huake'] = $this->input->post('is_allow_huake');
            	$data['update_date'] = date('Y-m-d H:i:s');
            	$bool = $this->nursery_model->update($data,array('id'=>$id));
            	if(empty($bool))throw new Exception('修改失败');
            	redirect("/admin/nursery");
            }
            $where = array();
            $where['id'] = $id;
            $where['status'] = 1;
            $nursery_info = $this->nursery_model->find($where);
            if(empty($nursery_info))throw new Exception('没有记录');
            
            $this->t->write_view('content', 'admin/nursery/update', array(
            		'nursery_info'	=>	$nursery_info,
            ), false);
            $this->t->render();

        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/admin/nursery/',
            ), false);
            $this->t->render();
        }
    }
    /**
     * 修改排序
     */
    public function sort(){
    	try {
    		$return = array('code'=>10,'msg'=>'修改成功');
    		$id = intval($this->input->get('id'));
    		if(empty($id))throw new Exception('ID为空');
    		$sort = intval($this->input->get('sort'));
    
    		$bool = $this->nursery_model->update(array('weight'=>$sort),array('id'=>$id));
    
    		if(empty($bool))throw new Exception('修改失败');
    
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);
    }
    /**
     * 修改状态
     */
    public function Updatelogin(){
    	try {
    		$return = array('code'=>10,'msg'=>'修改成功');
    		$id = intval($this->input->get('id'));
    		if(empty($id))throw new Exception('ID为空');
    		
    		$status = intval($this->input->get('status'));
    		
    		$type = intval($this->input->get('type'));
    		if(empty($type))throw new Exception('TYPE为空');

    		$data = array('is_allow_login'=>$status);
    		
    		if($type == 2)$data = array('is_allow_huake'=>$status);
    		
    		$bool = $this->nursery_model->update($data,array('id'=>$id));
    
    		if(empty($bool))throw new Exception('修改失败');
    
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);
    }
    /**
     * 添加
     */
    public function Add(){
    	try {
    		$form_submit = $this->input->post('form_submit');
    		if(!empty($form_submit)){
    			$data = array();
    			$data['area_id'] = intval($this->input->post('area_id'));
    			$data['city_id'] = intval($this->input->post('city_id'));
    			$data['title'] = $this->input->post('title');
    			$data['address'] = $this->input->post('address');
    			$data['explain'] = $this->input->post('explain');
    			$data['telephone'] = $this->input->post('telephone');
    			$data['is_allow_login'] = $this->input->post('is_allow_login');
    			$data['is_allow_huake'] = $this->input->post('is_allow_huake');
    			$data['create_date'] = date('Y-m-d H:i:s');
    			$bool = $this->nursery_model->insert($data);
    			if(empty($bool))throw new Exception('添加失败');
    			redirect("/admin/nursery");
    		}
    		
    		$this->t->write_view('content', 'admin/nursery/add', array(
    				
    		), false);
    		$this->t->render();
    
    	}catch (Exception $e){
    		$error = $e->getMessage();
    		$this->t->write_view('content', 'admin/msg_error', array(
    				'error'     =>  $error,
    				'time'  =>  10,
    				'url'   =>  '/admin/nursery/',
    		), false);
    		$this->t->render();
    	}
    }
    /**
     * 删除
     */
    public function Delete(){
    	try {
    		$id = intval($this->input->get('id'));
    		if(empty($id))throw new Exception('ID为空');
    
    		$bool = $this->nursery_model->update(array('status'=>0),array('id'=>$id));
    		if(empty($bool))throw new Exception('删除失败');
    
    	}catch (Exception $e){
    		$error = $e->getMessage();
    		$this->t->write_view('content', 'admin/msg_error', array(
    				'error'     =>  $error,
    				'time'  =>  10,
    				'url'   =>  '/admin/nursery/',
    		), false);
    		$this->t->render();
    	}
    	redirect("/admin/nursery");
    }
    /**
     * 获取地区
     */
    public function Getarea(){
    	try{
    		$return = array('code'=>10,'msg'=>'成功','list'=>array());
    		$list = $this->area_model->findAll(array('status'=>1),'id,title');
    		if(empty($list))throw new Exception('不存在');
    		$return['list'] = $list;
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);
    }
    /**
     * 获取市区
     */
    public function Getcity(){
    	try{
    		$return = array('code'=>10,'msg'=>'成功','list'=>array());
    		$area_id = intval($this->input->get('area_id'));
    		if(empty($area_id))throw new Exception('地区ID为空');
    		$list = $this->city_model->findAll(array('status'=>1,'area_id'=>$area_id),'id,title');
    		if(empty($list))throw new Exception('不存在');
    		$return['list'] = $list;
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);
    }
    
    
    
}






























